//
//  MySetViewController.m
//  OneWordNew
//
//  Created by qianfeng on 15-7-6.
//  Copyright (c) 2015年 朱共营. All rights reserved.
//

#import "MySetViewController.h"
#import "RegeditViewController.h"
#import "LoginViewController.h"
#import "SetViewController.h"
#import "SDImageCache.h"
#import "MyPageViewController.h"
@interface MySetViewController ()

@end

@implementation MySetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //[self.tabBarController.tabBar setHidden:YES];
    self.automaticallyAdjustsScrollViewInsets=NO;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed: @"top"] forBarMetrics:UIBarMetricsDefault];
    //[self creatClear];
}
- (void)creatClear{
    
    
    UIBarButtonItem *clear=[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"clear"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(setClick:)];
    
    self.navigationItem.rightBarButtonItem=clear;

}
//- (void)viewWillLayoutSubviews{
//    NSString *token=[[NSUserDefaults standardUserDefaults] objectForKey:@"token"];
//    NSString *name=[[NSUserDefaults standardUserDefaults] objectForKey:@"nickname"];
//    
//    if (token!=nil&&name!=nil) {
//        MyPageViewController *page=[[MyPageViewController alloc] init];
//        [self.navigationController pushViewController:page animated:NO];
//    }
//
//
//}
//- (void)viewWillAppear:(BOOL)animated{
//    NSString *token=[[NSUserDefaults standardUserDefaults] objectForKey:@"token"];
//    NSString *name=[[NSUserDefaults standardUserDefaults] objectForKey:@"nickname"];
//    
//    if (token!=nil&&name!=nil) {
//        MyPageViewController *page=[[MyPageViewController alloc] init];
//        [self.navigationController pushViewController:page animated:NO];
//    }
//
//
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)setClick:(UIButton *)sender {
    
    UIAlertController *sheet = [UIAlertController alertControllerWithTitle:@"清除缓存" message:[NSString stringWithFormat:@"总共有%.2fM缓存",[self getCachSize]] preferredStyle:UIAlertControllerStyleActionSheet];
    //增加按钮
    [sheet addAction:[UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        //删除
        //删除两部分
        //1.删除 sd 图片缓存
        //先清除内存中的图片缓存
        [[SDImageCache sharedImageCache] clearMemory] ;
        //清除磁盘的缓存
        [[SDImageCache sharedImageCache] clearDisk];
        
        //2.删除自己缓存
        NSString *myCachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/MyCaches"];
        [[NSFileManager defaultManager] removeItemAtPath:myCachePath error:nil];
        
        
        
    }]];
    [sheet addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        //取消
    }]];
    //跳转
    [self presentViewController:sheet animated:YES completion:nil];
    
    
    
}
- (CGFloat)getCachSize {
    //爱限免 有两类 缓存  sdWebImage 和 首页
    //获取图片缓存大小
    NSUInteger imageCacheSize = [[SDImageCache sharedImageCache] getSize];
    //获取自定义缓存大小
    //用枚举器遍历 一个文件夹的内容
    //1.获取 文件夹枚举器
    //
    
    NSString *myCachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/MyCaches"];
    NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:myCachePath];
    __block NSUInteger count = 0;
    //2.遍历
    for (NSString *fileName in enumerator) {
        NSString *path = [myCachePath stringByAppendingPathComponent:fileName];
        NSDictionary *fileDict = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
        count += fileDict.fileSize;//自定义所有缓存大小
    }
    // 得到是字节  转化为M
    CGFloat totalSize = ((CGFloat)imageCacheSize+count)/1024/1024;
    return totalSize;
}


- (IBAction)loginClick:(UIButton *)sender {
   // LoginViewController *login=[[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
    LoginViewController *login=[[LoginViewController alloc] init];
    [self.navigationController pushViewController:login animated:YES];
    
}

- (IBAction)regeditClick:(UIButton *)sender {
    RegeditViewController *regedit=[[RegeditViewController alloc] initWithNibName:@"RegeditViewController" bundle:nil];
    [self.navigationController pushViewController:regedit animated:YES];
}
@end
